/*
 * 
 * 从MrOHomeFragment 跳转进来，这里是选择小O形象页面
 * 
 * */
package com.shanghaionstar.mro;

import android.app.Activity;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.DealerBrandType;
import com.shanghaionstar.mro.view.MrOFootLayout;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.FinalConstants;


public class MrOChooseFigueFragment extends MrOBaseFragment implements OnClickListener, MrOFootLayout.OnFootViewConfirmButtonListener {
    @FindViewById(background = R.drawable.mro_choose_default)
    private CustomImageView mro_mr;//CustomImageView
    @FindViewById(background = R.drawable.mro_choose_buick)
    private CustomImageView mro_bk;
    @FindViewById(background = R.drawable.mro_mr_big)
    private CustomImageView mro_fig;
    @FindViewById(background = R.drawable.mro_choose_wtk)
    private CustomImageView mro_wtk;
    @FindViewById(background = R.drawable.mro_btton)
    private CustomImageView iv_btn_center, iv_btn_left, iv_btn_right;
    private View view;
    @FindViewById
    private TextView tv_mr, tv_bk, tv_wtk;// 字体

    private MrOActivity mroActivity;
    private DealerBrandType myBrand;

    @FindViewById
    private View nn_bk;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mroActivity = (MrOActivity) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.mro_choose_figue, container, false);
            injectView(view);
        } else {
            super.onResumeView(view);
        }
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        mroActivity.showBottomConfirmBtn(true);
        mroActivity.showBottomRightBtn(false);
    }

    @Override
    public void onStop() {
        super.onStop();
        mroActivity.showBottomConfirmBtn(false);
        mroActivity.showBottomRightBtn(true);
    }

    // 初始化
    private void initview() {
//		mro_mr = (CustomImageView) view.findViewById(R.id.mro_mr);
//		mro_bk = (CustomImageView) view.findViewById(R.id.mro_bk);
//		mro_fig = (CustomImageView) view.findViewById(R.id.mro_fig);
//		mro_wtk = (CustomImageView) view.findViewById(R.id.mro_wtk);
//		iv_btn_center = (CustomImageView) view.findViewById(R.id.iv_btn_center);
//		iv_btn_left = (CustomImageView) view.findViewById(R.id.iv_btn_left);
//		iv_btn_right = (CustomImageView) view.findViewById(R.id.iv_btn_right);
//		tv_mr = (TextView) view.findViewById(R.id.tv_mr);
//		tv_bk = (TextView) view.findViewById(R.id.tv_bk);
//		tv_wtk = (TextView) view.findViewById(R.id.tv_wtk);
        mro_bk.setOnClickListener(this);
        mro_mr.setOnClickListener(this);
        mro_wtk.setOnClickListener(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        initview();
        myBrand = DealerBrandType.getDealerBrandTypeByDesc(ApplicationUtil.getInstance().getCarInfo());
//        myBrand = DealerBrandType.DEFAULT;
        if(myBrand == DealerBrandType.DEFAULT) {
            nn_bk.setVisibility(View.GONE);
            iv_btn_center.setVisibility(View.GONE);
        }
        checkFigureConfig();
    }

    private void checkFigureConfig() {
        tv_bk.setText(DealerBrandType.getBriefNameByDesc(ApplicationUtil.getInstance().getCarInfo()));
        StateListDrawable stateListDrawable = null;
        switch (myBrand) {
            case CHEVROLET:
                stateListDrawable = (StateListDrawable) mroActivity.getResources().getDrawable(R.drawable.mro_choose_chevrolet);
                break;
            case BUICK:
                stateListDrawable = (StateListDrawable) mroActivity.getResources().getDrawable(R.drawable.mro_choose_buick);
                break;
            case CADILLAC:
                stateListDrawable = (StateListDrawable) mroActivity.getResources().getDrawable(R.drawable.mro_choose_cadillac);
                break;
            case DEFAULT:

                break;
        }
        mro_bk.setImageDrawable(stateListDrawable);
        String currentFigure = PreferencesUtls.getInstance(mroActivity).getString(FinalConstants.MroConstants.MRO_FIGURE_KEY);
        if ("".equals(currentFigure) || FinalConstants.MroConstants.MRO_FIGURE_DEFAULT.equals(currentFigure)) {
            refreshFigure(DealerBrandType.DEFAULT);
        } else if (FinalConstants.MroConstants.MRO_FIGURE_BRAND.equals(currentFigure)) {
            refreshFigure(myBrand);
        } else if (FinalConstants.MroConstants.MRO_FIGURE_WTK.equals(currentFigure)) {
            refreshFigure(DealerBrandType.WTK);
        }
    }

    private void refreshFigure(DealerBrandType figureType) {
        setView();
        setText();
        switch (figureType) {
            case DEFAULT:
                iv_btn_left.setVisibility(View.VISIBLE);
                tv_mr.setTextColor(android.graphics.Color.WHITE);
                mro_fig.setImageDrawable(getResources().getDrawable(R.drawable.mro_mr_big));
                mro_mr.setSelected(true);
                break;
            case BUICK:
                iv_btn_center.setVisibility(View.VISIBLE);
                tv_bk.setTextColor(android.graphics.Color.WHITE);
                mro_fig.setImageDrawable(getResources().getDrawable(R.drawable.mro_buick_big));
                mro_bk.setSelected(true);
                break;
            case CADILLAC:
                iv_btn_center.setVisibility(View.VISIBLE);
                tv_bk.setTextColor(android.graphics.Color.WHITE);
                mro_fig.setImageDrawable(getResources().getDrawable(R.drawable.mro_cadillac_big));
                mro_bk.setSelected(true);
                break;
            case CHEVROLET:
                iv_btn_center.setVisibility(View.VISIBLE);
                tv_bk.setTextColor(android.graphics.Color.WHITE);
                mro_fig.setImageDrawable(getResources().getDrawable(R.drawable.mro_chevrolet_big));
                mro_bk.setSelected(true);
                break;
            case WTK:
                iv_btn_right.setVisibility(View.VISIBLE);
                tv_wtk.setTextColor(android.graphics.Color.WHITE);
                mro_fig.setImageDrawable(getResources().getDrawable(R.drawable.mro_wtk_big));
                mro_wtk.setSelected(true);
                break;
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.mro_mr:// 默认形象
                refreshFigure(DealerBrandType.DEFAULT);
                break;
            case R.id.mro_bk:// 别克形象
                refreshFigure(myBrand);
                break;
            case R.id.mro_wtk:// 无头盔形象
                refreshFigure(DealerBrandType.WTK);
                break;
        }

    }

    // 箭头隐现
    private void setView() {
        iv_btn_left.setVisibility(View.INVISIBLE);

        if(myBrand == DealerBrandType.DEFAULT) {
            iv_btn_center.setVisibility(View.GONE);
        }else {
            iv_btn_center.setVisibility(View.INVISIBLE);
        }
        iv_btn_right.setVisibility(View.INVISIBLE);
        mro_mr.setSelected(false);
        mro_bk.setSelected(false);
        mro_wtk.setSelected(false);
    }

    // 字体颜色隐现
    private void setText() {
        tv_mr.setTextColor(this.getResources().getColor(R.color.mro_color));
        tv_bk.setTextColor(this.getResources().getColor(R.color.mro_color));
        tv_wtk.setTextColor(this.getResources().getColor(R.color.mro_color));
    }

    // 5.0 crash
    @Override
    public void onDestroy() {
        super.onDestroy();
        view = null;
    }

    @Override
    public void onConfirmButtonClick() {
        if (iv_btn_left.getVisibility() == View.VISIBLE)
            PreferencesUtls.getInstance(mroActivity).putString(FinalConstants.MroConstants.MRO_FIGURE_KEY, FinalConstants.MroConstants.MRO_FIGURE_DEFAULT);
        else if (iv_btn_center.getVisibility() == View.VISIBLE)
            PreferencesUtls.getInstance(mroActivity).putString(FinalConstants.MroConstants.MRO_FIGURE_KEY, FinalConstants.MroConstants.MRO_FIGURE_BRAND);
        else if (iv_btn_right.getVisibility() == View.VISIBLE)
            PreferencesUtls.getInstance(mroActivity).putString(FinalConstants.MroConstants.MRO_FIGURE_KEY, FinalConstants.MroConstants.MRO_FIGURE_WTK);
    }
}
